<?php
if( !defined( 'HIGHLIGHT_LINK' ) ) {
	define( 'HIGHLIGHT_LINK', 'images' );
}

if( KF_EDITOR_MODE == 'Markdown' ) {
	include_once( Foundry::config('libPath').'/markdown.php' );
} else {
	include_once( Foundry::config('libPath').'/Textile.php' );
}

class net_mode3_imageBucket_imageBucketAdminController extends FoundryControllerInterface
{


	/**
	 * init()
	 *
	 * init() is called in place of the __construct() method used by the
	 * FoundryControllerInterface super class
	 * This function will be called immediately after the __construct method
	 *
	 */ 
	public function init()
	{
//		$model = FoundryController::model( 'net.mode3.news:news' );	
	
	}
	
	public function admin()
	{

		return true;
	}
	
	public function upload()
	{
		if( $_FILES['imageFile'] ) {
			if( strpos( $_FILES['imageFile']['type'], 'image' ) !== false ) {
				$img = FoundryController::model( 'net.mode3.imageBucket:image' );
				
				$name = $img->cleanName( $_FILES['imageFile']['name'] );
				
				$img->name = $name;
				$img->added = time();
				
				$img->save();
				
				if( $img->uid ) {
					header( "Location: ".FoundryView::helperLinkTo( 'imageBucketAdmin/edit/'.$img->uid ) );
					exit;
				}
			}			
			return array( 'upload' => 'false' );
		}
	}
	
	public function edit()
	{
		$user = FoundryRegistry::get( 'USER_session');
		$id = FoundryRequest::get( 'id', 'num' );
		
		$image = false;
		
		$postTags = array();
		
		if( !$id || !is_numeric( $id ) ) {
			$id = false;
		} else {
			$image = FoundryController::model( 'net.mode3.imageBucket:image', $id );
			
			foreach( $image->tags as $tag ) {
				$postTags[] = $tag->uid;
			}
		}
		
		if( $user->checkKey() ) {
			$credit = cleanString( FoundryRequest::post( 'imageCredit', 'alnum' ) );
			$source = cleanString( FoundryRequest::post( 'imageSource', 'alnum' ) );
			$caption = cleanString( FoundryRequest::post( 'imageCaption', 'alnum' ) );
			$description = cleanString( FoundryRequest::post( 'imageDescription', 'alnum' ) );
			$tags = FoundryRequest::post( 'imageTagID', 'valid_array' );
						
			$published = FoundryRequest::post( 'imagePublished', 'num' );
			
			if( $id === false ) {
				$image = FoundryController::model( 'net.mode3.imageBucket:image' );
			}
			
			$image->credit = $credit;
			$image->caption = $caption;
			$image->source = $source;
			$image->description = $description;
			
			if( KF_EDITOR_MODE == 'Markdown' ) {
				$image->caption_markdown = Markdown( $image->caption );
				$image->description_markdown = Markdown( $image->description );
			} else {
				$textile = new Textile;
				$caption = preg_replace(
					"/&quot;([a-zA-Z0-9\-_ ]*)&quot;\:/",
					'"$1":',
					$image->caption );

				$description = preg_replace(
					"/&quot;([a-zA-Z0-9\-_ ]*)&quot;\:/",
					'"$1":',
					$image->description );
			
			
				$image->caption_markdown = $textile->process( $caption );
				$image->description_markdown = $textile->process( $description );
			}
						
			if( $published == 1 ) {
				$image->published = 1;
			} else {
				$image->published = 0;
			}

			
			$image->save();
		
			if( is_array( $tags ) && !empty( $tags ) ) {
				$image->setManyReference( 'tags', $tags );
			}
		
			header( "Location: ".
				FoundryView::helperLinkTo( 'imageBucketAdmin/edit/'.$image->uid.'/sv' ) );
			exit;
					
				
		}
		
		$key = $user->generateKey();
	
		return array( 'key' => $key, 'image' => $image, 'postTags' => $postTags );
	}
	
	public function publish()
	{
		$id = FoundryRequest::post( 'id', 'num' );
		
		if( $id ) {
			$image = FoundryController::model( 'net.mode3.imageBucket:image', $id );
			$image->published = 1;
			
			$image->save();
			
			return array( 'saved' => 1 );
		}
	}
	
	public function deleteImage()
	{
		$id = FoundryRequest::post( 'id', 'num' );
		
		if( $id ) {
			$image = FoundryController::model( 'net.mode3.imageBucket:image', $id );
			
			$image->delete();
			
			return array( 'saved' => 1 );
		}
	}
	
	public function rotateImage()
	{
		$id = FoundryRequest::post( 'id', 'num' );
		$rotate = FoundryRequest::post( 'rotate', 'num' );
		
		
		if( $id && $rotate ) {
			$image = FoundryController::model( 'net.mode3.imageBucket:image', $id );

			$image->gd_rotateImage( $image->filePathSystem.$image->thumbNail, $rotate );
			return array( 'saved' => 1 );
		}
	
	}
	
	public function cropImage()
	{
		$id = FoundryRequest::get( 'id', 'num' );
		$image = FoundryModel::factory( 'net.mode3.imageBucket:image', $id );
		
		if( $image->uid ) {
			return array( 'image' => $image );
		}
	}


}

?>